package com.culture.bootdemo.算法.leetcode;

import java.util.regex.Pattern;

/**
 * @Author: wen
 * @DateTime: 2025-04-15 13:55
 * @Description: 时间复杂度：O(n)
 * 空间复杂度：O(1)（原地修改）
 **/
public class 移除元素 {

    public static int removeElement(int[] nums, int val) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right - 1];
                right--;
            } else {
                left++;
            }
        }
        return left;
    }


    public static void main(String[] args) {
        int[] nums = {3, 2, 2, 3, 4, 3, 6};
        int val = 3;

        int k = removeElement(nums, val);

        System.out.println("剩余元素数量: " + k);
        System.out.print("新数组内容: ");
        for (int i = 0; i < k; i++) {
            System.out.print(nums[i] + " ");
        }
    }
}
